package com.zhiwang.xiaoqu.ui

import android.os.Bundle
import android.text.Editable
import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.hiray.exception.ExpiredException
import com.hiray.exception.XQException
import com.tencent.bugly.crashreport.CrashReport
import com.umeng.analytics.MobclickAgent
import com.zhiwang.xiaoqu.App
import com.zhiwang.xiaoqu.BuildConfig
import com.zhiwang.xiaoqu.bus.LogoutEvent
import com.zhiwang.xiaoqu.mvp.model.sharepref.PrefManager
import com.zhiwang.xiaoqu.mvp.view.AuthView
import com.zhiwang.xiaoqu.widget.Toasty
import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.support.HasSupportFragmentInjector
import io.reactivex.disposables.Disposable
import org.greenrobot.eventbus.EventBus
import quickStartActivity
import suicideAfterTransition
import javax.inject.Inject

open class BaseActivity : AppCompatActivity(), AuthView,HasSupportFragmentInjector {

    override fun supportFragmentInjector(): AndroidInjector<Fragment> = dispatchingAndroidInjector

    @Inject
    lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Fragment>

    open fun initInjection() {}

    fun getAppComponent() = (application as App).appComponent

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initInjection()
        //Android 8.0 不允许非全屏Activity固定屏幕方向
//        if (Build.VERSION.SDK_INT != 26)
//            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    }

    override fun onError(t: Throwable) {
        if (t is ExpiredException) {
            PrefManager.clearToken()
            onAuthExpired()
        } else if (t is XQException) {
            t.message?.apply { Toasty.message(this) }
        } else {
            CrashReport.postCatchedException(t)
        }
    }

    override fun onAuthExpired() {
        EventBus.getDefault().post(LogoutEvent())
        quickStartActivity(PhoneLoginActivity::class.java)
        suicideAfterTransition()
    }

    override fun onResume() {
        super.onResume()
        if (BuildConfig.DEBUG.not())
            MobclickAgent.onResume(this)
    }


    override fun onPause() {
        super.onPause()
        if (BuildConfig.DEBUG.not())
            MobclickAgent.onPause(this)
    }

    /**
     * 主要是为了方便监听EditText 的输入变化，来控制一些按钮的enable 状态来改变按钮的背景；
     * +由于很多地方都有用到，所以提取到了BaseActivity
     */
    fun observeTextChangeWithTargetButtonEnableStatus(views: Array<EditText>, target: View) {

        val textWatcher = object : TextWatcherAdapter() {
            override fun afterTextChanged(s: Editable?) {
                super.afterTextChanged(s)
                views.forEach {
                    if (it.text.isEmpty()) {
                        target.isEnabled = false
                        target.alpha = 0.5f
                        return
                    }
                }
                target.isEnabled = true
                target.alpha = 1f
            }
        }

        views.forEach {
            it.addTextChangedListener(textWatcher)
        }
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return super.onSupportNavigateUp()
    }

    private var disposableCollection: ArrayList<Disposable> = ArrayList()

    fun addToDiposable(disposable: Disposable?) {
        if (disposable != null && disposable.isDisposed.not())
            disposableCollection.add(disposable)
    }

    override fun onDestroy() {
        super.onDestroy()
        for (disposable in disposableCollection) {
            if (disposable.isDisposed.not())
                disposable.dispose()
        }
    }
}
